#pragma once
#include<iostream>
#include<vector>
#include<deque>
using namespace std;

namespace my
{
	template<class T, class Compare = less<T>, class Container = vector<T>>
	class priority_queue
	{
	public:
		priority_queue() = default;

		template<class InputInterator>
		priority_queue(InputInterator first, InputInterator last)
			:_con(first, last)
		{
			for (int i = (_con - 2) / 2; i >= 0; i--)
			{
				AdjustDown(i);
			}
		}

		void push(const T& x)
		{
			_con.push_back(x);
			AdjustUp(_con.size() - 1);
		}

		void pop()
		{
			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();
			AdjustDown(0);
		}

		void AdjustUp(int child)
		{
			Compare cmp;

			int parent = (child - 1) / 2;
			while (child > 0)
			{
				
				if (cmp(_con[parent], _con[child]))
				{
					swap(_con[child], _con[parent]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}

		void AdjustDown(int parent)
		{
			Compare cmp;

			int child = parent * 2 + 1;
			while (child < _con.size())
			{
				if (child + 1 < _con.size() && cmp(_con[child], _con[child + 1]))
				{
					child++;
				}

				if (cmp(_con[parent], _con[child]))
				{
					swap(_con[parent], _con[child]);
					parent = child;
					child = parent * 2 + 1;
				}
				else
				{
					break;
				}
			}
		}

		const T& top() const
		{
			return _con[0];
		}

		bool empty() const
		{
			return _con.empty();
		}

		size_t size()
		{
			return _con.size();
		}

	private:
		Container _con;
	};
}